package com.wudl.govern.service.impl;

import com.wudl.govern.domain.DbSource;
import com.wudl.govern.mapper.MetaStatisticsMapper;
import com.wudl.govern.service.MetaStatisticsService;
import com.wudl.govern.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：wudl
 * @date ：Created in 2022-04-05 1:08
 * @description：
 * @modified By：
 * @version: 1.0
 */

@Slf4j
@Service
public class MetaStatisticsServiceImpl implements MetaStatisticsService {


    @Autowired
    MetaStatisticsMapper metaStatisticsMapper;

    @Override
    public List<Object> selectRetrievalLibraryList(Map map) {
        try {
            if (map != null) {
                Integer dbSourceId = Integer.parseInt(map.get("dbSourceId").toString());
                if (dbSourceId != null) {
                    List<Object> list = new ArrayList();
                    //表的实体类
                    ResultTableContents resultTableContents = new ResultTableContents();
                    //视图的实体类
                    ResultViewContents resultViewContents = new ResultViewContents();
                    //存储过程的实体类
                    ResultProcContents resultProcedurePRContents = new ResultProcContents();
                    //函数的实体类
                    ResultFunctionContents resultProcedureFUContents = new ResultFunctionContents();
                    //表的数量
                    int tableSum = metaStatisticsMapper.selectRetrievalTable(dbSourceId);
                    resultTableContents.setName("表");
                    resultTableContents.setValue(tableSum);
                    list.add(resultTableContents);
                    //视图的数量
                    int viewSum = metaStatisticsMapper.selectRetrievalView(dbSourceId);
                    resultViewContents.setName("视图");
                    resultViewContents.setValue(viewSum);
                    list.add(resultViewContents);
                    //存储过程的数量
                    int ProcedurePRSum = metaStatisticsMapper.selectRetrievalProcedurePR(dbSourceId);
                    resultProcedurePRContents.setName("存储过程");
                    resultProcedurePRContents.setValue(ProcedurePRSum);
                    list.add(resultProcedurePRContents);
                    //函数的数量
                    int ProcedureFUSum = metaStatisticsMapper.selectRetrievalProcedureFU(dbSourceId);
                    resultProcedureFUContents.setName("函数");
                    resultProcedureFUContents.setValue(ProcedureFUSum);
                    list.add(resultProcedureFUContents);

                    return list;
                }

            }

            return null;

        } catch (Exception e) {
            log.error("##### 查询执行记录列表异常 ##### error={}", e);
            return null;
        }
    }


    /**
     * 饼状图
     *
     * @param map
     * @return
     */
    @Override
    public List<Object> selectRetrievalCake(Map map) {
        try {
            if (map != null) {
                Integer dbSourceId = Integer.parseInt(map.get("dbSourceId").toString());
                if (dbSourceId != null) {
                    List<Object> list = new ArrayList();
                    //表的实体类
                    ResultTableContents resultTableContents = new ResultTableContents();
                    //视图的实体类
                    ResultViewContents resultViewContents = new ResultViewContents();
                    //存储过程的实体类
                    ResultProcContents resultProcedurePRContents = new ResultProcContents();
                    //函数的实体类
                    ResultFunctionContents resultProcedureFUContents = new ResultFunctionContents();
                    //表的数量
                    int tableSum = metaStatisticsMapper.selectRetrievalTable(dbSourceId);
                    resultTableContents.setName("表");
                    resultTableContents.setValue(tableSum);
                    list.add(resultTableContents);
                    //视图的数量
                    int viewSum = metaStatisticsMapper.selectRetrievalView(dbSourceId);
                    resultViewContents.setName("视图");
                    resultViewContents.setValue(viewSum);
                    list.add(resultViewContents);
                    //存储过程的数量
                    int ProcedurePRSum = metaStatisticsMapper.selectRetrievalProcedurePR(dbSourceId);
                    resultProcedurePRContents.setName("存储过程");
                    resultProcedurePRContents.setValue(ProcedurePRSum);
                    list.add(resultProcedurePRContents);
                    //函数的数量
                    int ProcedureFUSum = metaStatisticsMapper.selectRetrievalProcedureFU(dbSourceId);
                    resultProcedureFUContents.setName("函数");
                    resultProcedureFUContents.setValue(ProcedureFUSum);
                    list.add(resultProcedureFUContents);
                    return list;
                }
            }
            return null;
        } catch (Exception e) {
            log.error("##### 查询执行记录列表异常 ##### error={}", e);
            return null;
        }
    }

    /**
     * 柱状图
     *
     * @param map
     * @return
     */
    @Override
    public List<Object> selectRetrievalColumn(Map maps) {
        try {
            List<DbSource> libraryList = selectRetrievalLibraryTool(maps);
            List list = new ArrayList();
            List listT = new ArrayList();
            List listV = new ArrayList();
            List listP = new ArrayList();
            List listF = new ArrayList();
            for (DbSource dbSource : libraryList) {
                //表的数量
                int tableSum = metaStatisticsMapper.selectRetrievalTable(dbSource.getDbSourceId());
                listT.add(tableSum);
                //视图的数量
                int viewSum = metaStatisticsMapper.selectRetrievalView(dbSource.getDbSourceId());
                listV.add(viewSum);
                //存储过程的数量
                int ProcedurePRSum = metaStatisticsMapper.selectRetrievalProcedurePR(dbSource.getDbSourceId());
                listP.add(ProcedurePRSum);
                //函数的数量
                int ProcedureFUSum = metaStatisticsMapper.selectRetrievalProcedureFU(dbSource.getDbSourceId());
                listF.add(ProcedureFUSum);
            }
            NormalVEO normalVEO = new NormalVEO();
            NormalEO normalEO = new NormalEO();
            //拼接返回格式
            normalEO.setShow(true);
            normalEO.setPosition("insideRight");
            normalVEO.setNormal(normalEO);

            //按echarts的json格式返回出去
            LibraryResultColumn columnEOB = new LibraryResultColumn();
            columnEOB.setName("表");
            columnEOB.setData(listT);
            columnEOB.setType("bar");
            columnEOB.setStack("总量");
            columnEOB.setLabel(normalVEO);
            LibraryResultColumn columnEOST = new LibraryResultColumn();
            columnEOST.setName("视图");
            columnEOST.setData(listV);
            columnEOST.setType("bar");
            columnEOST.setStack("总量");
            columnEOST.setLabel(normalVEO);
            LibraryResultColumn columnEOCCGC = new LibraryResultColumn();
            columnEOCCGC.setName("存储过程");
            columnEOCCGC.setData(listP);
            columnEOCCGC.setType("bar");
            columnEOCCGC.setStack("总量");
            columnEOCCGC.setLabel(normalVEO);
            LibraryResultColumn columnEOHS = new LibraryResultColumn();
            columnEOHS.setName("函数");
            columnEOHS.setData(listF);
            columnEOHS.setType("bar");
            columnEOHS.setStack("总量");
            columnEOHS.setLabel(normalVEO);

            list.add(columnEOB);
            list.add(columnEOST);
            list.add(columnEOCCGC);
            list.add(columnEOHS);


            return list;
        } catch (Exception e) {
            log.error("##### 查询执行记录列表异常 ##### error={}", e);
            return null;
        }
    }

    /**
     * 元数据统计’柱形数据库名称‘查询功能
     *
     * @param maps
     * @return
     */
    @Override
    public List<DbSource> selectRetrievalLibrary(Map maps) {
        try {
            //获取库信息名称,id
            List<DbSource> libraryList = selectRetrievalLibraryTool(maps);
            return libraryList;
        } catch (Exception e) {
            log.error("##### 查询执行记录列表异常 ##### error={}", e);
            return null;
        }
    }

    /**
     * 根据分页添加进行查询数据
     */
    public List<DbSource> selectRetrievalLibraryTool(Map maps) {
        try {
            int pageNumber = 1;
            int pageSize = 10;
            if (maps.get("pageNumber") != null && maps.get("pageNumber") != "") {
                pageNumber = Integer.parseInt(maps.get("pageNumber").toString());
            }
            if (maps.get("pageSize") != null && maps.get("pageSize") != "") {
                pageSize = Integer.parseInt(maps.get("pageSize").toString());
            }
            //计算得到从哪条数据开始
            int pages = pageNumber * pageSize - pageSize;
            if (pages < 0) {
                pages = 0;
            }
            //获取库信息名称,id
            return metaStatisticsMapper.selectRetrievalLibrary(pages, pageSize);
        } catch (Exception e) {
            log.error("##### 查询执行记录列表异常 ##### error={}", e);
            return null;
        }
    }
}
